<template>
  <div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
      <el-form-item label="游戏名称">
        <el-input v-model="game.name" />
      </el-form-item>
      
      <el-form-item label="游戏热度">
        <el-input-number v-model="game.heat" :min="0"/>
      </el-form-item>
      <el-form-item label="注册人数">
        <el-input-number v-model="game.registerPerson" :min="0"/>
      </el-form-item>
      <el-form-item label="公司名称">
        <el-input v-model="game.company"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
import gameApi from "@/api/game/game";
export default {
    data() {
        return {
            game:{
                heat: 0,
                registerPerson: 0
            }
        }
    },
    created() {
      if(this.$route.params.id){
        const id = this.$route.params.id;
        this.fetchDataById(id)
      }
    },
    methods: {
      fetchDataById(id){
        gameApi.getGameById(id)
        .then(response => {
          this.game = response.data
        })
      },
      //添加
      save(){
        gameApi.saveGame(this.game)
        .then((response) => {
            this.$message({
                type: 'success',
                message: '添加成功!'
            });
            //跳转列表页面
            this.$router.push({path:'/game/list'})
        })
      },
      //修改
      update(){
        gameApi.updateGame(this.game)
        .then((response) => {
            this.$message({
                type: 'success',
                message: '修改成功!'
            });
            //跳转列表页面
            this.$router.push({path:'/game/list'})
        })
      },
      saveOrUpdate(){
        if(this.game.id){
          this.update()
        }else{
          this.save()
        }
      }
    },
    
}
</script>